﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GeneralClasses;

namespace Interface
{
    public partial class frmChooseSchedule : Form
    {
        int currentSchedule;
        List<Schedule> schedules;
        Form returnTo;

        public frmChooseSchedule(List<Schedule> Schedules, Form ReturnTo)
        {
            returnTo = ReturnTo;
            schedules = Schedules;
            currentSchedule = 0;
            InitializeComponent();
        }

        void refresh()
        {
            lblCurrent.Text = Convert.ToString(currentSchedule + 1) + " / " +
                Convert.ToString(schedules.Count);

            List<Person> people = Person.loadAllActiveNurses();

            clmEmployee.Items.Clear();
            foreach (Person item in people)
	        {
                clmEmployee.Items.Add(item.FullName);
	        }

            dgvSchedule.Rows.Clear();
            foreach (GeneralClasses.Day item in schedules[currentSchedule].Days)
            {
                foreach (GeneralClasses.Shift current in item.getAllShifts())
                {
                    string skills = "";
                    foreach (Skill skill in current.Skills)
                    {
                        skills += skill.Name = " ";
                    }

                    /*if (current.Departments.Count > 0)
                    {
                        dgvSchedule.Rows.Add(current.getAssignedPersons()[0].FullName,
                            current.Name, current.StartTime.ToString(), current.EndTime.ToString(),
                            1, current.Departments[0].Name, skills);
                    }
                    else
                    {
                        dgvSchedule.Rows.Add(current.getAssignedPersons()[0].FullName,
                            current.Name, current.StartTime.ToString(), current.EndTime.ToString(),
                            1, "", skills);
                    }*/
                }
            }
        }

        private void btnPreviousChoice_Click(object sender, EventArgs e)
        {
            currentSchedule--;
            if (currentSchedule < 0)
            {
                currentSchedule = schedules.Count - 1;
            }
        }

        private void btnNextChoice_Click(object sender, EventArgs e)
        {
            currentSchedule++;
            if (currentSchedule >= schedules.Count)
            {
                currentSchedule = 0;
            }
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            refresh();
        }

        private void btnUseCurrent_Click(object sender, EventArgs e)
        {
            Schedule useSchedule = new Schedule();

            //put schedule data from dgvSchedule into useSchedule

            if (useSchedule.saveData() <= 0)
            {
                MessageBox.Show("Successfully saved schedule.");
                this.Close();
            }
            else
            {
                MessageBox.Show("Problem saving schedule!");
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmChooseSchedule_Shown(object sender, EventArgs e)
        {
            refresh();
        }

        private void frmChooseSchedule_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult result = MessageBox.Show("Are you sure you want to not use one of these schedules?",
                "Quit?", MessageBoxButtons.YesNo);

            if (result == DialogResult.No)
            {
                e.Cancel = true;
            }
            else
            {
                returnTo.Show();
            }
        }
    }
}
